package java_test.collection;

import java.util.*;

public class CollectionSize {
    public static void main(String[] args) {
        int size;
        HashSet collection = new HashSet();
        String str1 = "Yellow", str2 = "White", str3 =
                "Green", str4 = "Blue";
        Iterator iterator;
        collection.add(str1);
        collection.add(str2);
        collection.add(str3);
        collection.add(str4);
        System.out.print("集合数据: ");
        iterator = collection.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        size = collection.size();
        if (collection.isEmpty()) {
            System.out.println("集合是空的");
        } else {
            System.out.println("集合长度: " + size);
        }

        List<Integer> List = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List.add(new Integer(i));
            System.out.println("打乱前:");
            System.out.println(List);
        }
        for (int i = 1; i < 6; i++) {
            System.out.println("第" + i + "次打乱：");
            //  Collections.shuffle() 方法来打乱集合元素的顺序
            Collections.shuffle(List);
            System.out.println(List);
        }

        // List集合的遍历
        listTest();
        // Set集合的遍历
        setTest();
    }

    private static void setTest() {
        Set<String> set = new HashSet<String>();
        set.add("JAVA");
        set.add("C");
        set.add("C++");
        // 重复数据添加失败
        set.add("JAVA");
        set.add("JAVASCRIPT");
        // 使用iterator遍历set集合
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            String value = it.next();
            System.out.println(value);
        }

        // 使用增强for循环遍历set集合
        for (String s : set) {
            System.out.println(s);
        }
    }

    private static void listTest() {
        List<String> list = new ArrayList<String>();
        list.add("菜");
        list.add("鸟");
        list.add("教");
        list.add("程");
        list.add("www.runoob.com");
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String value = it.next();
            System.out.println(value);
        }
        for (int i = 0, size = list.size(); i < size; i++) {
            String value = list.get(i);
            System.out.println(value);
        }
        // 使用增强for循环进行遍历
        for (String value : list) {
            System.out.println(value);
        }
    }
}
